package cn.dansj.common.utils.spring.context;

import cn.dansj.common.processor.meta.WriteToResource;
import cn.dansj.common.utils.spring.context.condition.ConditionalOnClass;
import org.springframework.boot.context.event.ApplicationContextInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

@Order(Ordered.HIGHEST_PRECEDENCE)
@WriteToResource
@ConditionalOnClass(ApplicationContextInitializedEvent.class)
public class ApplicationContextListener implements ApplicationListener<ApplicationContextInitializedEvent> {
    @Override
    public void onApplicationEvent(ApplicationContextInitializedEvent applicationContextInitializedEvent) {
        ConfigurableApplicationContext applicationContext = applicationContextInitializedEvent.getApplicationContext();
        ApplicationContextProvider.injectApplicationContext(applicationContext);
        // 重新刷新配置信息
        if (applicationContext.isActive()) GetEnvironment.getEnvironmentMapData();
    }
}